package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class TalkThread extends Thread {
	private Socket s;

	public TalkThread(Socket s) {
		this.s = s;
	}

	@Override
	public void run() {
		System.out.println("ip:" + s.getInetAddress() + "，本地端口："
				+ s.getLocalPort() + "，远程端口：" + s.getPort());

		try {
			InputStream in = s.getInputStream();
			OutputStream out = s.getOutputStream();
			PrintWriter pw = new PrintWriter(out);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String str = null;
			while (true) {
				str = br.readLine();// 读取一行。读到\r\n结束
				System.out.println(str);
				if (str == null || str.equals("quit"))
					break;

				pw.println("您所说的" + str + ",朕知道了");
				pw.flush();

			}
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			try {
				s.close();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				// e.printStackTrace();
			}
		}

	}
}
